package com.example.quartz.common.base;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.Serializable;
import java.util.List;

/**
 * @description:
 * @author: wlp
 * @createDate: 2020-01-08
 * @version: 1.0
 */
public class BaseController<T extends Serializable, P extends BaseMapper<T>> {

    @Autowired
    private P mapper;

    @PostMapping("list")
    @ApiOperation(value = "查询列表数据", httpMethod = "POST")
    public List<T> list(T bean) {
        EntityWrapper<T> wrapper = new EntityWrapper<>();
        wrapper.setEntity(bean);
        return mapper.selectList(wrapper);
    }

    @PostMapping("save")
    @ApiOperation(value = "保存数据", httpMethod = "POST")
    public boolean save(T bean) {
        Integer row = mapper.insert(bean);
        return row == 1;
    }

    @PostMapping("update")
    @ApiOperation(value = "更新数据", httpMethod = "POST")
    public boolean update(T bean) {
        Integer row = mapper.updateById(bean);
        return row == 1;
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除数据", httpMethod = "POST")
    public boolean delete(Long id) {
        Integer row = mapper.deleteById(id);
        return row == 1;
    }

    @PostMapping("selectByPrimaryKey")
    @ApiOperation(value = "根据ID获取数据", httpMethod = "POST")
    public T selectByPrimaryKey(Long id) {
        return mapper.selectById(id);
    }

}
